import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        Connection connection =dataSource.getConnection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改的同学学号：");
        int id = scanner.nextInt();
        System.out.println("请输入你要把该同学的姓名改成什么：");
        //next读到空白符（空格，制表符，翻页符，换行符，回车符，垂直制表符……）
        //nextLine读到换行符
        String name = scanner.next();
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        System.out.println(statement);
        int n = statement.executeUpdate();
        System.out.println("n = "+n);
        statement.close();
        connection.close();
    }
}
